﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;

public partial class UserCenter_user_Comment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)// 判断是否已经评价，防止重复评分
        {
            if (Session["Rating1"] != null)
            {
                ifRating(Rating1, Label1);
            }
            if (Session["Rating2"] != null)
            {
                ifRating(Rating2, Label2);
            }
            if (Session["Rating3"] != null)
            {
                ifRating(Rating3, Label3);
            }

            if (!object.Equals(Request.QueryString["Detail_Idint"], null))
            {
                BindData();
            }
        }
    }
    public void BindData()
    {
        try
        {
            BLL.Business.OrderBLL orderBLL = new BLL.Business.OrderBLL();
            int DetailId = Int32.Parse(Request.QueryString["Detail_Idint"].ToString());
            DataSet ds = orderBLL.SelectDetail(DetailId);
            if ((ds == null) ||
                   (ds.Tables.Count == 0) ||
                   (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
            {
                return;
            }
            else
            {
                this.productId.Text = ds.Tables[0].Rows[0]["Book_Idint"].ToString();
                this.userId.Text = ds.Tables[0].Rows[0]["User_Idint"].ToString();
                this.txtName.Text = Session["UserName"].ToString();
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    private void ifRating(Rating r, Label l)
    {
        r.CurrentRating = int.Parse(Session[r.ID].ToString());// 显示评价结果
        l.Text = Session[r.ID].ToString();
        r.ReadOnly = true;                                    // 不能再评价
    }
    protected void Rating_Changed1(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        RatingChange(Rating1, Label1, e.Value);
    }
    protected void Rating_Changed2(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        RatingChange(Rating2, Label2, e.Value);
    }
    protected void Rating_Changed3(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        RatingChange(Rating3, Label3, e.Value);
    }
    private void RatingChange(Rating r, Label l, string mark)
    {
        Session.Add(r.ID, mark);// session中记录本次评价，防止多次评价
        r.ReadOnly = true;      // 不可在评价
        l.Text = mark;          // 显示本次评价的结果
        Ratingrecord rr = new Ratingrecord();
        string clas = string.Empty;// 被评价的条目
        switch (r.ID)              // 获取被评价的条目的名称
        {
            case "Rating1": clas = "服务态度"; break;
            case "Rating2": clas = "宝贝相符"; break;
            case "Rating3": clas = "发货速度"; break;
        }
        rr.Insert(clas, mark);     // 记录这次评价
    }
    // 添加评论（修改订单详情表）
    protected void btnReView_Click(object sender, EventArgs e)
    {
        try
        {
            BLL.Business.CommentBLL commBLL = new BLL.Business.CommentBLL();
            BLL.Business.OrderBLL orderBLL = new BLL.Business.OrderBLL();
            Model.Comment comm = new Model.Comment();
            int DetailId = Int32.Parse(Request.QueryString["Detail_Idint"].ToString());

            comm.User_Idint = Int32.Parse(this.userId.Text.ToString());
            comm.Book_Idint = Int32.Parse(this.productId.Text.ToString());
            comm.Comm_Captionstr = "评论";
            comm.Comm_Detailtext = this.txtContent.Text.ToString();
            comm.Comm_PublishDatedate = DateTime.Now;
            comm.Comm_Rankint = 5;

            if (commBLL.Insert(comm))
            {
                this.btnReView.Enabled = false;
                orderBLL.UpdatreOrderDetail(DetailId, 1);
            }
            else
            {
                Response.Write("<script>alert('出现问题，请查正！')</script>");
                return;
            }
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
}
